package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-12 09:35
 **/
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;


    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) throws IOException {
        Result<Map> result = listFeignClient.list(searchParam);
        Map resultData = result.getData();
        // 我们需要返回给页面的数据
        // searchParam  一些条件值
        model.addAttribute("searchParam", searchParam);


        // urlParam 拼接条件查询
        String urlParam = this.makeUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        // trademarkParam 品牌面包蟹回显
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam", trademarkParam);
        // propsParamList 平台属性面包蟹回显
        List<Map> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("propsParamList", propsParamList);
        // orderMap 排序处理
        Map<String, Object> orderMap = this.makeOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);
        // trademarkList
        // attrsList
        // goodsList
        // pageNo
        // totalPages
        // 以上的属性统一都在我们返回的数据有所显示
        model.addAllAttributes(resultData);

        return "list/index";
    }

    /**
     * 排序处理
     *
     * @param order
     * @return
     */
    private Map<String, Object> makeOrderMap(String order) {
        HashMap<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                //     1:hotScore 2:price
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
            return map;
        } else {
            // 如果没有排序的话  默认情况是什么
            map.put("type", 1);
            map.put("sort", "asc");
            return map;
        }
    }

    /**
     * 平台属性的回显
     *
     * @param props
     * @return
     */
    private List<Map> makePropsParamList(String[] props) {
         List<Map> mapList = new ArrayList<>();
        // 判断为空
        if (props != null && props.length > 0) {

            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length > 0) {
                    //props=23:4G:运行内存
                    HashMap<String, String> map = new HashMap<>();
                    map.put("attrName", split[2]);
                    map.put("attrValue", split[1]);
                    map.put("attrId", split[0]);
                    mapList.add(map);
                }
            }
            // 返回数据
            return mapList;
        }
        // 默认返回空
        return null;
    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuffer urlParam = new StringBuffer();
        // 首先判断查询条件是否为空
        if (!StringUtils.isEmpty(searchParam)) {
            // 首先判断分类id
            Long category1Id = searchParam.getCategory1Id();
            if (category1Id != null && category1Id > 0) {
                // 拼接链接
                urlParam.append("category1Id=" + category1Id);
            }
            Long category2Id = searchParam.getCategory2Id();
            if (category2Id != null && category2Id > 0) {
                // 拼接链接
                urlParam.append("category2Id=" + category2Id);
            }
            Long category3Id = searchParam.getCategory3Id();
            if (category3Id != null && category3Id > 0) {
                // 拼接链接
                urlParam.append("category3Id=" + category3Id);
            }

            // 然后判断关键字
            String keyword = searchParam.getKeyword();
            if (!StringUtils.isEmpty(keyword)) {
                urlParam.append("keyword=").append(keyword);
            }

            // 然后判断品牌的拼接
            String trademark = searchParam.getTrademark();
            if (!StringUtils.isEmpty(trademark)) {
                //2:华为
                urlParam.append("&trademark=" + trademark);
            }

            // 然后判断平台属性进行拼接
            String[] paramProps = searchParam.getProps();
            if (paramProps != null && paramProps.length > 0) {
                for (String paramProp : paramProps) {
                    //    23:4G:运行内存
                    urlParam.append("&props=" + paramProp);
                }
            }
            return "list.html?" + urlParam.toString();
        }

        return "";
    }

    /**
     * trademarkParam 品牌回显处理
     *
     * @param trademark
     * @return
     */
    private String makeTrademarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");

            if (split != null && split.length == 2) {

                String trademarkParam = "品牌:" + split[1];

                return trademarkParam;
            }
        }

        // 默认没有
        return "";
    }

}
